<template>
  <div>
    <el-row class="top" style="height: 160px">
      <!-- 商城logo -->
      <div style="float: left">
        <img src="../assets/logo.jpg" alt="" style="height: 150px" />
      </div>
      <!-- 搜索框 -->
      <div style="float: left; margin-left: 5%">
        <el-input
          placeholder="请输入内容"
          style="width: 400px; line-height: 150px"
        ></el-input>
        <!-- 搜索按钮 -->
        <el-button type="danger" icon="el-icon-search"></el-button>
        <!-- 购物车 -->
        <el-badge :value="0" class="item">
          <el-button
            icon="el-icon-shopping-cart-full"
            style="margin-left: 10%"
            @click="gocart"
            >购物车</el-button
          >
        </el-badge>
      </div>
    </el-row>
    <!-- 分类 -->
    <el-card class="soft" header="品类推荐">
      <div>
        <!-- 服饰鞋包分类 -->
        <a href="/clothes"><img src="../assets/1.jpg" width="18%" alt="" /></a>
        <!-- 家居生活分类 -->
        <a href="/homelife"
          ><img
            src="../assets/2.jpg"
            width="18%"
            alt=""
            style="margin-left: 2%"
        /></a>
        <!-- 电子数码分类 -->
        <a href="/electronic"
          ><img
            src="../assets/3.jpg"
            width="18%"
            alt=""
            style="margin-left: 2%"
        /></a>
        <!-- 电子书分类 -->
        <a href="/book"
          ><img
            src="../assets/4.jpg"
            width="18%"
            alt=""
            style="margin-left: 2%"
        /></a>
        <!-- 食品分类 -->
        <a href="/food"
          ><img
            src="../assets/5.jpg"
            width="18%"
            alt=""
            style="margin-left: 2%"
        /></a>
      </div>
    </el-card>
    <el-main>
      <!-- 轮播图 -->
      <el-carousel :interval="4000" height="500px">
        <el-carousel-item v-for="item in imagebox" :key="item.id">
          <img :src="item.idView" class="image" width="100%" height="inherit" />
        </el-carousel-item>
      </el-carousel>
      <!-- 商品 -->
      <h1>为你推荐</h1>
      <!-- 先把所有商品列出来 然后随机显示多少个 -->
      <!-- //循环体 -->
      <div v-for="item in goodsList" :key="item.id">
        <el-card class="goods">
          <!-- 跳转事件 -->
          <el-link
            @click="goodsDetails(item.id)"
            target="_blank"
            :underline="false"
          >
            <img :src="item.goodsImage" width="100%" height="180px" alt="" />
            <!-- 三级分类+ 商品名 + sku（未实现） -->
            <span>{{ item.category3Name }} {{ item.goodsName }}</span>
            <!-- 价格 -->
            <br /><br /><span style="color: red">￥{{ item.newPrice }}</span>
          </el-link>
        </el-card>
      </div>
    </el-main>
  </div>
</template>

<script>
import { Goods } from "../api/goods";
export default {
  data() {
    return {
      imagebox: [
        { id: 0, idView: require("../assets/6.jpg") },
        { id: 1, idView: require("../assets/7.jpg") },
        { id: 2, idView: require("../assets/8.jpg") },
      ],
      screenWidth: 0,
      goodsList: [],
    };
  },
  methods: {
    setSize: function () {
      // 通过浏览器宽度(图片宽度)计算高度
      this.bannerHeight = (400 / 1920) * this.screenWidth;
    },
    gocart() {
      this.$router.push("/cart");
    },
    //数组对象去重
    unique(arr) {
      const res = new Map();
      return arr.filter((arr) => !res.has(arr.id) && res.set(arr.id, 1));
    },
    goodsDetails(id) {
      // console.log(id);
      //获取对应商品详情
      this.$router.push(`/details?id=${id}`);
    },
  },
  mounted() {
    // 首次加载时,需要调用一次
    this.screenWidth = window.innerWidth;
    this.setSize();
    // 窗口大小发生改变时,调用一次
    window.onresize = () => {
      this.screenWidth = window.innerWidth;
      this.setSize();
    };
    Goods().then(({ data }) => {
      var goods = data.data.data;
      for (var i = 0; i <= 8; i++) {
        var rand = Math.round(Math.random(goods.length) * 10);
        // console.log(i);
        // console.log(rand);
        // console.log(goods);

        this.goodsList.push(goods[rand]);
      }

      this.goodsList = this.unique(this.goodsList);
      this.goodsList.forEach((item) => {
        item.goodsImage = this.$host + item.goodsImage;
      });
      console.log(this.goodsList);
    });
  },
};
</script>

<style>
.top {
  width: 60%;
  margin: auto;
}
body {
  margin: 0px !important;
  background-color: white;
}
/* 分类 */
.soft {
  width: 60%;
  height: 30%;
  margin: auto;
}
.el-main {
  background-color: #e9eef3;
  color: #333;
  width: 60%;
  margin: auto;
  min-height: calc(100vh - 120px);
}
h1 {
  text-align: center;
  /* background-color: white; */
}
/* 商品 */
.goods {
  width: 24%;
  height: 300px;
  float: left;
  margin-left: 0.5%;
  margin-top: 1%;
}
</style>